<template>
	<view class="detail">
		<view class="title">
			{{detail.title}}
		</view>
		<view class="info">
			<view class="author">编辑：{{detail.author}}</view>
			<view class="time">发布时间：{{detail.posttime}}</view>
		</view>
		<view class="content">
			<rich-text :nodes="detail.content"></rich-text>
		</view>
		
	</view>
</template>

<script>
	import {parseTime} from '@/utils/tool.js'
	export default {
		data() {
			return {
				option: '',
				detail:{},
			};
		},
		onLoad(e) {
			this.option = e,
				this.getDetail()
		},
		methods: {
			getDetail() {
				uni.request({
					url: 'https://ku.qingnian8.com/dataApi/news/detail.php',
					data: this.option,
					success: res => {
						console.log(res);
						res.data.content=res.data.content.replace(/<img/gi,'<img style="max-width:100%"')
						res.data.posttime=parseTime(res.data.posttime)
						this.detail=res.data
						this.saveHistory()
						uni.setNavigationBarTitle({
							title:this.detail.title
						})
					}
				})
			},
			saveHistory(){
				let historyArr=uni.getStorageSync('historyArr') || []
				let item={
					id:this.detail.id,
					looktime:parseTime(Date.now()),
					classid:this.detail.classid,
					picurl:this.detail.picurl,
					title:this.detail.title
				}
				let index=historyArr.findIndex((i)=>i.id===this.detail.id)
				console.log(index);	
				if(index>=0){
					historyArr.splice(index,1)
				}
				if(historyArr){
				historyArr.unshift(item)	
				}
				historyArr=historyArr.slice(0,10)
				uni.setStorageSync('historyArr',historyArr)
			}
		}
	}
</script>

<style lang="scss">
	.detail {
		padding: 30rpx;

		.title {
			font-size: 46rpx;
			color: #333;
		}

		.info {
			background: #f6f6f6;
			padding: 20rpx;
			font-size: 25rpx;
			color: #666;
			display: flex;
			justify-content: space-between;
			margin: 40rpx 0;
		}

		.content {
			padding-bottom: 50rpx;
			/deep/ img{
				max-width: 100%;
			}

		}
	}
</style>
